#include <machine.h>
#include <arch/arm.h>
#include <errno.h>

#ifdef __TOP_HALF__

struct machine_desc *machine_lookup(void)
{
	struct machine_desc *md;
	uint32_t cpuid;

	cpuid = get_cpuid_id();
	for_each_machine_desc(md) {
		if ((cpuid & md->cpu_mask) == md->cpu_val) {
				return md;
		}
	}

	return ERR_PTR(-ENODEV);
}

#else

static struct machine_desc *machine = NULL;

struct machine_desc *machine_lookup(void)
{
	struct machine_desc *md;
	uint32_t cpuid;

	if (!machine) {
		cpuid = get_cpuid_id();
		for_each_machine_desc(md) {
			if ((md->match && !md->match(cpuid, md))
					|| (!md->match && ((cpuid & md->cpu_mask) == md->cpu_val))) {
				machine = md;
				return md;
			}
		}
	} else {
		return machine;
	}

	return ERR_PTR(-ENODEV);
}

#endif

